﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace PLCS.App.Shared.Views.UserControls.Chlidren
{
    /// <summary>
    /// AgvStation.xaml 的交互逻辑
    /// </summary>
    public partial class AgvStation : UserControl
    {
        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.Register("Command", typeof(ICommand), typeof(AgvStation), new UIPropertyMetadata(null));

        public object CommandParameter
        {
            get { return (object)GetValue(CommandParameterProperty); }
            set { SetValue(CommandParameterProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CommandParameter.  This enables animation,styling,binding,etc...
        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.Register("CommandParameter", typeof(object), typeof(AgvStation), new UIPropertyMetadata(null));

        /// <summary>
        /// 强制输入事件和属性来自FrameworkElement
        /// </summary>
        public IInputElement CommandTarget
        {
            get { return (IInputElement)GetValue(CommandTargetProperty); }
            set { SetValue(CommandTargetProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CommandTarget.  This enables animation,etc...
        public static readonly DependencyProperty CommandTargetProperty =
            DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(AgvStation), new UIPropertyMetadata(null));

        /// <summary>
        /// 名称
        /// </summary>
        public string SName
        {
            get { return (string)GetValue(SNameProperty); }
            set { SetValue(SNameProperty, value); }
        }

        public static readonly DependencyProperty SNameProperty =
            DependencyProperty.Register("SName", typeof(string), typeof(AgvStation), new PropertyMetadata("S101"));

        public AgvStation()
        {
            InitializeComponent();
            SizeChanged += ConveyorLine_SizeChanged;
        }

        /// <summary>
        /// 重载鼠标左击时触发事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            //_eventAggregator.GetEvent<StationEvent>().Publish(SName);
            var command = Command;
            var parameter = CommandParameter;
            var target = CommandTarget;

            var routedCmd = command as RoutedCommand;
            if (routedCmd != null && routedCmd.CanExecute(parameter, target))
            {
                routedCmd.Execute(parameter, target);
            }
            else if (command != null && command.CanExecute(parameter))
            {
                command.Execute(parameter);
            }
        }

        private void ConveyorLine_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Cursor = Cursors.Hand; //小手
            ToolTip = $"AGV接驳位:{SName}";
            Border border = new Border();
            border.Background = Brushes.White;
            border.Width = this.Width;
            border.Height = this.Height;
            border.BorderBrush = Brushes.DimGray;
            border.BorderThickness = new Thickness(4);

            Grid grid = new Grid();
            Path path1 = new Path();
            path1.Stroke = Brushes.DimGray;
            path1.StrokeThickness = 4;
            PathGeometry pathGeometry1 = new PathGeometry();
            PathFigure pathFigure1 = new PathFigure();
            pathFigure1.StartPoint = new Point(0, 0);
            LineSegment lineSegment1 = new LineSegment();
            lineSegment1.Point = new Point(this.Width, this.Height);
            pathFigure1.Segments.Add(lineSegment1);
            pathGeometry1.Figures.Add(pathFigure1);
            path1.Data = pathGeometry1;

            Path path2 = new Path();
            path2.Stroke = Brushes.DimGray;
            path2.StrokeThickness = 4;
            PathGeometry pathGeometry2 = new PathGeometry();
            PathFigure pathFigure2 = new PathFigure();
            pathFigure2.StartPoint = new Point(0, this.Height - 8);
            LineSegment lineSegment2 = new LineSegment();
            lineSegment2.Point = new Point(this.Width - 8, 0);
            pathFigure2.Segments.Add(lineSegment2);
            pathGeometry2.Figures.Add(pathFigure2);
            path2.Data = pathGeometry2;

            grid.Children.Add(path1);
            grid.Children.Add(path2);

            border.Child = grid;

            this.Content = border;
        }
    }
}